home *** CD-ROM | disk | FTP | other *** search
- /* Copyright 1992 H.Ogasawara(COR.) */
-
- #include "proto.h"
- #include "run.h"
-
- /*
- X-MODEM128
- X-MODEM1024
- Y-MODEM
- Y-MODEM-batch
- */
-
- #define GWAIT 8 /*(20*100) /*8*/
-
- unsigned short top= 0,
- blockn= 0;
-
- WritePack( pack, len )
- unsigned char *pack;
- {
- while( len-- ){
- if( Write232C( *pack++ ) < 0 )
- return -1;
- }
- return 0;
- }
-
- SendEnd()
- {
- EndProgram();
- }
-
- void
- SendRun2( ptr )
- unsigned char *ptr;
- {
- if( ptr && *ptr == ACK ){
- CloseFile();
- timeout= runsize= 0;
- SendEnd();
- return;
- }
- Write232C( CAN );
- allbreak();
- }
-
- void
- SendRun1( ptr )
- unsigned char *ptr;
- {
- unsigned char pack[2048],
- buf[2048];
- static int len;
- static unsigned short chkmode= 0;
- if( !ptr ){
- if( chkmode == __G ){
- if( (len= ReadFile( buf, firstsize )) < 0 ){
- Write232C( EOT );
- run= SendRun2;
- goto lasttime;
- }
- block+= len>>7;
- size+= len;
- len= DataPack( ++blockn & 0xff, pack, buf, chkmode == NAK ? len+0x8000 : len );
- if( WritePack( pack, len ) < 0 ){
- allbreak();
- return;
- }
- DispCountup();
- run= SendRun1;
- timeout= GWAIT+ONTIME();
- runsize= 0;
- }else{
- Write232C( CAN );
- allbreak();
- }
- }else{
- run= SendRun1;
- if( top == 1 ){
- if( (chkmode= *ptr) == NAK ){
- top= 0;
- goto datasend;
- }else if( *ptr == __C || *ptr == __G ){
- if( *ptr == __C && rettype < 2 ){
- top= 0;
- goto datasend;
- }
- SetFilePacket( buf, fnamenode(filename), filesize, filetime );
- len= DataPack( 0, pack, buf, 128 );
- top= *ptr == __C ? 2 : 3;
- goto writepack;
- }else
- *ptr= CAN;
- }else if( top == 3 ){
- if( *ptr == __C || *ptr == __G ){
- top= 0;
- goto datasend;
- }else
- *ptr= CAN;
- }else{
- if( *ptr == NAK ){
- /* Error */
- }else if( *ptr == ACK ){
- datasend:
- if( top == 2 ){
- top= 3;
- goto lasttime;
- }
- if( (len= ReadFile( buf, firstsize )) < 0 ){
- Write232C( EOT );
- run= SendRun2;
- goto lasttime;
- }
- block+= len>>7;
- size+= len;
- len= DataPack( ++blockn & 0xff, pack, buf, chkmode == NAK ? len+0x8000 : len );
- DispCountup();
- }else
- *ptr= CAN;
- }
- if( *ptr == CAN ){
- Write232C( CAN );
- allbreak();
- return;
- }
- writepack:
- if( WritePack( pack, len ) < 0 ){
- allbreak();
- return;
- }
- if( top == 0 && chkmode == __G ){
- timeout= GWAIT+ONTIME();;
- runsize= 0;
- return;
- }
- lasttime:
- timeout= 6000+ONTIME();
- runsize= 1;
- }
- }
-
- void
- SendStart()
- {
- blockn= block= size= last= 0;
- if( OpenFileR( filename ) < 0 )
- return;
- run= SendRun1;
- top= 1;
- firstsize= rettype == 0 ? 128 : 1024;
- timeout= 6000+ONTIME();
- while( ISNS232C() )
- INP232C();
- runsize= 1;
- }
-
-